<template>
  <div class="unarranged-container">
    
    <div class="top-actions">
   
    
        <el-form :inline="true" class="search-form">
          <el-form-item label="授权名称" class="el-form-item">
            <el-input v-model="searchForm.username" placeholder="请输入授权名称" clearable />
          </el-form-item>
          <el-form-item label="考生姓名" class="el-form-item">
            <el-input v-model="searchForm.candidateName" placeholder="请输入考生姓名" clearable />
          </el-form-item>

          <el-form-item label="证件号码" class="el-form-item">
            <el-input v-model="searchForm.idNumber" placeholder="请输入证件号码" clearable />
          </el-form-item>
          <el-form-item label="报考专业" class="el-form-item">
            <el-select v-model="searchForm.major" placeholder="请选择报考专业" clearable>
              <el-option v-for="item in majorData" :key="item" :label="item" :value="item" />
            </el-select>
          </el-form-item>
          <el-form-item label="报考等级" class="el-form-item">
            <el-select v-model="searchForm.level" placeholder="请选择报考等级" clearable>
              <el-option v-for="item in levelData" :key="item" :label="item" :value="item" />
            </el-select>
          </el-form-item>
          <el-form-item label="报考日期" class="el-form-item">
            <el-date-picker v-model="searchForm.applicationDate" type="date" placeholder="请选择报考日期" value-format="YYYY-MM-DD" />
          </el-form-item>
          <!-- <el-form-item label="报考等级" class="el-form-item">
            <el-select v-model="searchForm.level" placeholder="请选择报考等级" clearable>
              <el-option label="1级" value="1级" />
              <el-option label="2级" value="2级" />
              <el-option label="3级" value="3级" />
              <el-option label="4级" value="4级" />
              <el-option label="5级" value="5级" />
              <el-option label="6级" value="6级" />
              <el-option label="7级" value="7级" />
              <el-option label="8级" value="8级" />
              <el-option label="9级" value="9级" />
              <el-option label="10级" value="10级" />
            </el-select>
          </el-form-item> -->
          <!-- <el-form-item label="授权名称" class="el-form-item">
            <el-select v-model="user_id" placeholder="请选择授权名称" clearable :disabled="institutionOptions.length === 1">
              <el-option v-for="item in institutionOptions" :key="item.id" :label="item.username" :value="item.id" />
            </el-select>
          </el-form-item> -->
          <!-- <el-form-item label="考级申报">
            <el-select v-model="searchForm.declarationId" placeholder="请选择考级申报" clearable>
              <el-option v-for="item in declarationOptions" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item> -->
          <el-form-item>
            <el-button type="primary" @click="handleSearch" class="Button">查询</el-button>
            <el-button @click="resetForm" class="Button">重置</el-button>
          </el-form-item>
          <el-form-item>
          <el-button type="primary" :disabled="!tableData.length" @click="handleExport" class="Button"> 导出未排考报表 </el-button>
        </el-form-item>
        </el-form>
       
     
    </div>

    <!-- 表格 -->
    <el-table v-loading="loading" :data="tableData" border class="el-table">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="candidateName" label="考生姓名" width="100" />
      <el-table-column prop="candidateNamePinyin" label="姓名拼音" width="160" />
      <el-table-column prop="gender" label="性别" width="60" />
      <el-table-column prop="major" label="报考专业" width="100" />
      <el-table-column prop="level" label="报考等级" width="100" />
      <el-table-column prop="ethnicity" label="民族" width="100" />
      <el-table-column prop="idType" label="证件类型" width="100" />
      <el-table-column prop="idNumber" label="证件号码" width="220" />

      <el-table-column prop="campus" label="所在校区" width="220" />
      <el-table-column prop="applicationDate" label="报考日期" width="180" />
      <el-table-column prop="currentAddress" label="现居地址" width="180" />
      <el-table-column label="操作" width="120" fixed="right">
        <template #default="{ row }">
          <el-button type="danger" size="small" class="mini-btn" @click="handleDelete(row.id)"> 删除 </el-button>
        </template>
      </el-table-column>

      <template #empty>
        <el-empty description="暂无数据" />
      </template>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[14, 28, 50, 100,200]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script setup>
  import { ref, reactive, onMounted, watch, computed } from 'vue';
  import { ElMessage, ElMessageBox } from 'element-plus';
  import { Plus, Document, Delete } from '@element-plus/icons-vue';
  import Http from '@/utils/http';
  import { areaOptions } from '@/utils/area-data';
  import OSS from 'ali-oss';
  import { imgUrls } from '@/utils/utils';
  import { exportRegistrationForm } from '@/utils/Api';
  import { getSubuser, getExamApplications } from '@/api/api';
  import { useMainStore } from '@/stores/useMain';
  const mainStore = useMainStore();

  const majorData = ref(['主持']);
  const levelData = ref(['1级', '2级', '3级', '4级', '5级', '6级', '7级', '8级', '9级', '10级']);

  // 搜索表单数据
  const searchForm = reactive({
    username: '', //  授权名称
    candidateName: '', // 考生姓名
    idNumber: '', // 证件号码
    major: '', // 报考专业
    level: '', // 等级
    applicationDate: '', // 报考日期
  });
  
  // 表格数据
  const tableData = ref([]);
  // 添加 loading 变量
  const loading = ref(false);

  // 分页数据
  const currentPage = ref(1);
  const pageSize = ref(14);
  const total = ref(100);

  // 机构选项
  const institutionOptions = ref([]);

  // 考级申报选项
  const declarationOptions = ref([]);
  // 生命周期钩子
  onMounted(() => {
    getInstitutions();
  });
  // 获取机构列表
  const getInstitutions = async () => {
    try {
      const data = await getSubuser();
      institutionOptions.value = data;

    
    
      getExamList();
      // 移除这里的 getExamList 调用，因为 watch(user_id) 会处理它
    } catch (error) {
      console.error('获取机构列表失败：', error);
    }
  };



  // 获取未安排考场的考生列表数据
  const getExamList = async () => {
    loading.value = true; // 开始加载
    try {
      const params = {
        page: currentPage.value,
        pageSize: pageSize.value,
      
        hasAdmissionTicket: false,
        ...searchForm
      };

      const res = await Http.post({
        url: '/yp_admin/applyList',
        data: params,
      });
      tableData.value = res.data.list;
      total.value = res.data.pagination.total;
    } catch (error) {
      console.error('获取考生列表失败：', error);
      ElMessage.error('获取考生列表失败');
    } finally {
      loading.value = false; // 结束加载
    }
  };

  // 重置表单
  const resetForm = () => {
    searchForm.username = '';
    searchForm.candidateName = '';
    searchForm.idNumber = '';
    searchForm.major = '';
    searchForm.level = '';
    searchForm.applicationDate = '';
    // 只在有多个机构选项时才重置 user_id
    
    handleSearch();
  };

  // 搜索方法
  const handleSearch = () => {
    currentPage.value = 1;
    getExamList();
  };

  const handleSizeChange = (val) => {
    pageSize.value = val;
    getExamList();
  };

  const handleCurrentChange = (val) => {
    currentPage.value = val;
    getExamList();
  };

  // 监听机构选择变化
  // watch(user_id, (newVal) => {
  //   if (newVal) {
      
  //     getDeclarationList();
  //     handleSearch();
  //   }
  // });

  // 监听考级申报选择变化
  // watch(
  //   () => searchForm.declarationId,
  //   () => {
  //     handleSearch();
  //   },
  // );

  // 添加删除方法
  const handleDelete = async (id) => {
    try {
      await ElMessageBox.confirm('确定要删除这条记录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      });

      await Http.post({
        url: '/yp_admin/deleteApply',
        data: { id },
      });

      ElMessage.success('删除成功');
      // 重新加载数据
      getExamList();
    } catch (error) {
      if (error !== 'cancel') {
        console.error('删除失败：', error);
        ElMessage.error('删除失败');
      }
    }
  };

  // 添加导出方法
  const handleExport = () => {
    const params = {
     
      hasAdmissionTicket: false,
      ...searchForm
    };
    exportRegistrationForm(params);
  };
</script>

<style scoped lang="scss">
 
  .el-form--inline .el-form-item {
    margin-right: 10px !important;
    :deep(.el-form-item__content){
      width: 180px !important;
      .el-input{
        height:100%;
      }
    }
  }
  .el-select,
  .el-input {
    width: 180px;
  }
  .unarranged-container {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    padding: 20px;
    overflow: hidden; // 防止溢出
    display: flex;
    flex-direction: column;
    .top-actions {
      margin-bottom: 20px;
      display: flex;
    
    }

    :deep(.el-input__wrapper) {
    height: 40px;
    line-height: 40px;
  }

  :deep(.el-form-item__label) {
    height: 40px;
    line-height: 40px;
    padding-right: 12px;
    white-space: nowrap;
  }

    .search-form {
      margin-bottom: 0;
      flex: 1;
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      gap: 10px;

      :deep(.el-form-item) {
        margin-bottom: 0;
        margin-right: 10px;

        &:last-child {
          margin-right: 0;
        }
      }
    }
  }
  .el-table {
    flex: 1;
    width: 100%;
    overflow: scroll;
  }
  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
</style>
